package com.atguigu.gmall.activity.controller;

import com.atguigu.gmall.activity.service.SeckillGoodsService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.order.model.OrderInfo;
import com.netflix.ribbon.proxy.annotation.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: atguigu
 * @create: 2023-03-17 14:49
 */
@RestController
@RequestMapping("/api/activity")
public class SeckillGoodsController {

    @Autowired
    private SeckillGoodsService seckillGoodsService;

    /**
     * 为当前用户生成当前抢购商品抢购码
     *
     * @param skuId
     * @param request
     * @return
     */
    @GetMapping("/seckill/auth/getSeckillSkuIdStr/{skuId}")
    public Result getSeckillSkuIdStr(@PathVariable("skuId") Long skuId, HttpServletRequest request) {
        //获取登录用户ID
        String userId = AuthContextHolder.getUserId(request);
        //生成抢购码
        return seckillGoodsService.getSeckillSkuIdStr(skuId, userId);
    }


    /**
     * 秒杀下单预处理(不处理真实秒杀业务,将秒杀购买意向发送MQ)
     *
     * @param request
     * @param skuId    购买商品
     * @param skuIdStr 抢购码
     * @return
     */
    @PostMapping("/seckill/auth/seckillOrder/{skuId}")
    public Result seckillPre(HttpServletRequest request, @PathVariable("skuId") Long skuId, @RequestParam("skuIdStr") String skuIdStr) {
        //获取用户ID
        String userId = AuthContextHolder.getUserId(request);
        return seckillGoodsService.seckillPre(userId, skuId, skuIdStr);
    }


    /**
     * 前端定时发起检查当前用户秒杀商品结果
     *
     * @param skuId 秒杀商品ID
     * @return
     */
    @GetMapping("/seckill/auth/checkOrder/{skuId}")
    public Result checkOrderResult(HttpServletRequest request, @PathVariable("skuId") Long skuId) {
        String userId = AuthContextHolder.getUserId(request);
        return seckillGoodsService.checkOrderResult(userId, skuId);
    }

    /**
     * 前端提交秒杀订单请求
     *
     * @param orderInfo
     * @return
     */
    @PostMapping("/seckill/auth/submitOrder")
    public Result<Long> submitSeckillOrder(HttpServletRequest request, @RequestBody OrderInfo orderInfo) {
        String userId = AuthContextHolder.getUserId(request);
        orderInfo.setUserId(Long.valueOf(userId));
        Long orderId = seckillGoodsService.submitSeckillOrder(orderInfo);
        return Result.ok(orderId);
    }
}
